//
//  WatersCenterController.m
//  Water
//
//  Created by yh on 2017/12/1.
//  Copyright © 2017年 chenguibang. All rights reserved.
//

#import "WatersCenterController.h"
#import "WatersCenterCell.h"
#import "WaterConversationViewController.h"
#import "RoomsCell.h"
#import "WatersItemCell.h"
#import "RCIMApi.h"
#import "UserBaseinfo.h"
#import "WaterCenterUser.h"
#import "UserProfileVC.h"
#import "RankController.h"
#import <Masonry/Masonry.h>
#import <UIColor+YYAdd.h>
#import <MJExtension/MJExtension.h>
@interface WatersCenterController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>{
    UILabel *bottomTip;
}

@end

@implementation WatersCenterController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"水吧";


    
    bottomTip = [[UILabel alloc]init];
    [self.view addSubview:bottomTip];
    [bottomTip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-49);
        make.height.equalTo(@30);
    }];
    bottomTip.textColor = UIColorHex(0x999999);
    bottomTip.font = [UIFont systemFontOfSize:12];
    bottomTip.textAlignment = NSTextAlignmentCenter;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 30)/2 ,(SCREEN_WIDTH - 30)/2 + 30);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"WatersItemCell" bundle:nil] forCellWithReuseIdentifier:@"WatersItemCell"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(64);
        make.bottom.equalTo(bottomTip.mas_top);
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [RCIMApi groupUserQueryWith:@"" progress:^(NSProgress *progress) {
        
    } success:^(ApiResponse *response) {
        
        
        self.memberIdList =  [WaterCenterUser mj_objectArrayWithKeyValuesArray:response.data[@"userBaseinfoList"]];
        bottomTip.text = [NSString stringWithFormat:@"%ld位水友已加入饮水计划",self.memberIdList.count];
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}
#pragma mark - UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
     return self.memberIdList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WatersItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WatersItemCell" forIndexPath:indexPath];
    WaterCenterUser *model = self.memberIdList[indexPath.row];
    cell.userBaseinfo = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //新建一个聊天会话View Controller对象,建议这样初始化
    
    WaterCenterUser *model = self.memberIdList[indexPath.row];
    UserProfileVC *userProfileVC = [[UserProfileVC alloc]init];
    userProfileVC.title = model.nickname;
    userProfileVC.userId = model.userId;
    userProfileVC.waterCenterUser = model;
    [self.navigationController pushViewController:userProfileVC animated:YES];
    
    
    
//    WaterConversationViewController *chat = [[WaterConversationViewController alloc] initWithConversationType:ConversationType_PRIVATE
//                                                                                                     targetId:model.userId];
//
//    chat.title = model.nickname;
//    //显示聊天会话界面
//    chat.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:chat animated:YES];
}


- (IBAction)toRankVC:(id)sender {
    
    RankController *rankVC = [[RankController alloc] initWithNibName:@"RankController" bundle:nil];
    rankVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:rankVC animated:YES];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
